package com.tianyiyan.tyenword;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class StaticData
{
	/// @brief Email
	private String _email;
	/// @brief Email Password
	private String _emailPasswd;
	/// @brief smtp Host
	private String _smtpHost;
	/// @brief smtp Port
	private String _smtpPort;
	/// @brief Server Host
	private String _serverHost;
	/// @brief Email Debug
	private String _mailDebug;
	/// @brief Clear Invalid User Cycle
	private Integer _clearCycle;
	/// @brief User Invalid Interval
	private Integer _userInvalidInterval;
	
	private static StaticData s_shareStaticData = null;
	/**
	 * @name config-properties keys
	 * @{
	 */
	public static final String KEY_EMAIL = "email";
	public static final String KEY_EMAIL_PASSWD = "emailPasswd";
	public static final String KEY_SMTP_HOST = "smtpHost";
	public static final String KEY_SMTP_PORT = "smtpPort";
	public static final String KEY_SERVER_HOST = "serverHost";
	public static final String KEY_MAIL_DEBUG = "mailDebug";
	public static final String KEY_CLEAR_CYCLE = "clearCycle";
	public static final String KEY_USER_INVALID_INTERVAL = "userInvalidInterval";
	/**@}*/
	/**
	 * @name default values
	 * @{
	 */
	public static final String DEFAULT_SMTP_HOST = "smtp.sina.com";
	public static final String DEFAULT_SMTP_PORT = "25";
	public static final String DEFAULT_SERVER_HOST = "http://localhost:8080";
	public static final String DEFAULT_CLEAR_CYCLE = "86400";
	public static final String DEFAULT_USER_INVALID_INTERVAL = "86400000";
	/**@}*/
	public static StaticData getInstance()
	{
		if (s_shareStaticData == null)
			s_shareStaticData = new StaticData();
		
		return s_shareStaticData;
	}
	
	private StaticData()
	{
		Properties propertie = new Properties(); 
        try
        {
        	String path = getClass().getClassLoader().getResource("").getPath();
        	FileInputStream inputFile = new FileInputStream(path + "config.properties"); 
        	propertie.load(inputFile);
            inputFile.close();
            
        	_email = propertie.getProperty(KEY_EMAIL, "");
        	_emailPasswd = propertie.getProperty(KEY_EMAIL_PASSWD, "");
        	_smtpHost = propertie.getProperty(KEY_SMTP_HOST, DEFAULT_SMTP_HOST);
        	_smtpPort = propertie.getProperty(KEY_SMTP_PORT, DEFAULT_SMTP_PORT);
        	_mailDebug = propertie.getProperty(KEY_MAIL_DEBUG, "false");
        	_clearCycle = Integer.parseInt(propertie.getProperty(KEY_CLEAR_CYCLE, DEFAULT_CLEAR_CYCLE));
        	_userInvalidInterval = Integer.parseInt(propertie.getProperty(KEY_USER_INVALID_INTERVAL, DEFAULT_USER_INVALID_INTERVAL));
        	
        	_serverHost = propertie.getProperty(KEY_SERVER_HOST, DEFAULT_SERVER_HOST);
        }
        catch (IOException ex)
        { 
        	System.err.println("Read Config File Faile!");
            ex.printStackTrace(); 
        }
	}
	
	public String getEmail(){	return _email;}
	public String getEmailPasswd(){	return _emailPasswd;}
	public String getSmtpHost(){ return _smtpHost;}
	public String getSmtpPort(){ return _smtpPort;}
	public String getServerHost(){ return _serverHost;}
	public String getMailDebug(){ return _mailDebug;}
	public Integer getClearCycle(){return _clearCycle;}
	public Integer getUserInvalidInterval(){return _userInvalidInterval;}
	
}
